package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Collect;
import com.khshop.service.CollectService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-16
 */
@RestController
@RequestMapping("/khshop/collect")
public class CollectController {

    @Autowired
    CollectService collectService;

    //新增收藏
    @ApiOperation("新增收藏")
    @PostMapping("/addCollect")
    @ResponseBody
    public AjaxResult addCollect(@RequestBody Collect collect) {
        if (collectService.addCollect(collect).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //修改收藏
    @ApiOperation("修改收藏")
    @PostMapping("/updateCollect")
    @ResponseBody
    public AjaxResult updateCollect(@RequestBody Collect collect) {
        if (collectService.updateCollect(collect).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //删除收藏
    @ApiOperation("删除收藏")
    @PostMapping("/delCollect")
    @ResponseBody
    public AjaxResult delCollect(Integer id) {
        if (collectService.delCollect(id).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //查询收藏
    @ApiOperation("查询收藏")
    @PostMapping("/searchCollect")
    @ResponseBody
    public AjaxResult searchCollect(@RequestBody Map<String, String> map,
                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", defaultValue = "15") Integer limit) {

        ServiceResult<IPage<Collect>> serviceResult = collectService.searchCollect(map, page, limit);
        if (serviceResult.isFlag()) {
            return AjaxResult.success((int) serviceResult.getData().getTotal(), serviceResult.getData());
        } else {
            return AjaxResult.error();
        }

    }
}

